//
//  UserProfileManager.m
//  huimei
//
//  Created by fengyn on 16/2/19.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "UserProfileManager.h"
#import "Services.h"

static UserProfileManager *sharedInstance = nil;
@interface UserProfileManager();
@property (strong, nonatomic) NSMutableDictionary *allUserInfoDictionary;
@property (strong, nonatomic) NSMutableDictionary *lodingUserInfoDictionary;
@end
@implementation UserProfileManager
+ (instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (NSMutableDictionary *)lodingUserInfoDictionary{
    if (!_lodingUserInfoDictionary) {
        _lodingUserInfoDictionary=[[NSMutableDictionary alloc] init];
    }
    return _lodingUserInfoDictionary;
}

- (NSMutableDictionary *)allUserInfoDictionary{
    if (!_allUserInfoDictionary) {
        _allUserInfoDictionary=[[NSMutableDictionary alloc] init];
    }
    return _allUserInfoDictionary;
}

- (void)loadUserInfoByUsername:(NSString *)username success:(void(^)(UserProfileEntity * entity))success{
    UserProfileEntity *entity= [self getUserProfileByUsername:username];
    if (entity) {
        success(entity);
        return;
    }
    if (self.lodingUserInfoDictionary[username]) {
        //当前用户信息正在加载中...
        return;
    }else{
        [self.lodingUserInfoDictionary setObject:username forKey:username];
    }
    [ServiceManager getUserInfoWithId:username success:^(NSDictionary *data) {
        [self.allUserInfoDictionary setObject:data forKey:username];
        UserProfileEntity *entity=[UserProfileEntity entityWithDic:data];
        success(entity);
        [self.lodingUserInfoDictionary removeObjectForKey:username];
    } failure:^{
        success(nil);
        [self.lodingUserInfoDictionary removeObjectForKey:username];
    }];
}

- (UserProfileEntity*)getUserProfileByUsername:(NSString*)username
{
    NSDictionary *dic=[self.allUserInfoDictionary objectForKey:username];
    return [UserProfileEntity entityWithDic:dic];
}

- (UserProfileEntity*)getCurUserProfile
{
    UserProfileEntity *entity = [[UserProfileEntity alloc] init];
    entity.username=[DataManager sharedInstance].userId;
//    entity.nickname=[DataManager sharedInstance].userNickName;
//    entity.imageUrl=[DataManager sharedInstance].userAvator;
    return entity;
}

- (NSString*)getNickNameWithUsername:(NSString*)username
{
    UserProfileEntity* entity = [self getUserProfileByUsername:username];
    if (entity.nickname && entity.nickname.length > 0) {
        return entity.nickname;
    } else {
        return username;
    }
}
@end

@implementation UserProfileEntity

+ (instancetype)entityWithDic:(NSDictionary*)dic;
{
    if (!dic) {
        return nil;
    }
    UserProfileEntity *entity = [[UserProfileEntity alloc] init];
    entity.objectId=dic[@"sequenceNBR"];
    entity.nickname=dic[@"nickName"];
    entity.username=dic[@"userId"];
    entity.imageUrl=dic[@"avatar"];
    return entity;
}

@end
